package google.docs;

import items.item_data;

import java.io.IOException;
import java.net.URL;
import java.util.List;

import shopping.manager.R;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.spreadsheet.CellEntry;
import com.google.gdata.data.spreadsheet.CellFeed;
import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
import com.google.gdata.data.spreadsheet.SpreadsheetFeed;
import com.google.gdata.data.spreadsheet.WorksheetEntry;
import com.google.gdata.util.ServiceException;

import date.base.DbHelper;


public class log_in extends Activity {
	
	private static final boolean DEVELOPER_MODE = true;
	private EditText UserName;
	private EditText Password;
	private EditText Sheet;
	private EditText List;
	public static String uName;
	public static String password;
	public static String sheetName = "Shopping List";
	public static String listName = "List 1";
	private DbHelper DbHelper;
	private SQLiteDatabase mDb;
	public static String list_name;
	public static final int TABLE_NAME_COLUMN = 1;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		// TODO Auto-generated method stub
		
		if (DEVELOPER_MODE) {
	         StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
	                 .detectDiskReads()
	                 .detectDiskWrites()
	                 .detectNetwork()   // or .detectAll() for all detectable problems
	                 .penaltyLog()
	                 .build());
	         StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
	                 .detectLeakedSqlLiteObjects()
	                 .detectLeakedClosableObjects()
	                 .penaltyLog()
	                 .penaltyDeath()
	                 .build());
	     }
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.log_in);
		
		UserName = (EditText) findViewById(R.id.userName);
		Password = (EditText) findViewById(R.id.password);
		Sheet = (EditText) findViewById(R.id.spreadSheetName);
		List = (EditText) findViewById(R.id.spreadSheetListName);
		
		Button b = (Button) findViewById(R.id.button1);
		
		b.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				uName = UserName.getText().toString();
				password = Password.getText().toString();
				sheetName = Sheet.getText().toString();
				listName = List.getText().toString();
				//users u = new users(uName,password);
				
				
				try {
					google_docs();
				} catch (ServiceException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				finish();
			}

		});
		
	}
	
	public void google_docs() throws ServiceException, IOException{

		//Toast.makeText(log_in.this, "email is : " + uName,Toast.LENGTH_LONG).show();
		//Toast.makeText(log_in.this, "password is : " + password,Toast.LENGTH_LONG).show();
		
		List<WorksheetEntry> worksheets = null;
        URL cellFeedUrl = null;
        
    
        SpreadsheetService service = new SpreadsheetService("tweat");
        try{
        	service.setUserCredentials(uName, password);
        }
        catch (Exception e){
        	Toast.makeText(log_in.this, "Log-In details are incorrect", Toast.LENGTH_LONG).show();
        	finish();
        }
        
        
        try{
	        URL metafeedUrl = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");
	        SpreadsheetFeed feed = service.getFeed(metafeedUrl, SpreadsheetFeed.class);
	
	        List<SpreadsheetEntry> spreadsheets = feed.getEntries();
	        for (int i = 0; i < spreadsheets.size(); i++) {
	            SpreadsheetEntry entry = spreadsheets.get(i);
	            if(entry.getTitle().getPlainText().equals(sheetName)){
	                    System.out.println("\t" + entry.getTitle().getPlainText());
	                    worksheets = entry.getWorksheets();
	            }
	            //	             System.out.println("\t" + entry.getTitle().getPlainText());
	        }
	    
	        for (int i = 0; i < worksheets.size(); i++) {
	              WorksheetEntry worksheet = worksheets.get(i);
	              String title = worksheet.getTitle().getPlainText();
	              if(title.equals(listName)){
	                      cellFeedUrl = worksheet.getCellFeedUrl();
	                      System.out.println("\tTitle: " + title);
	                      DbHelper = new DbHelper(this);
	                      DbHelper.open();
	                      list_name = DbHelper.createList2(listName);
	                      
	                      DbHelper.close();
	                      
	                      System.out.println("List Table Name: " + list_name);
		            	  
		            	 
		          }
	              
	              CellFeed feed1 = service.getFeed(cellFeedUrl, CellFeed.class);
	              int j=0;
	              String[] items = new String[4];
            	  for (CellEntry cell : feed1.getEntries()) {
	            	  if(j<4){
	            		  System.out.println("j: " + j);
	            		  System.out.println("name : " + cell.getCell().getValue());
	            		  items[j] = cell.getCell().getValue().toString();
	            		  //System.out.println("items of j : " + items[j]);
	            	  }
	            	  if(j==4){
		            	  System.out.println("Hey hiii");
		            	  item_data itemData = new item_data(list_name,items[0],items[1],items[2],items[3]);
		            	  System.out.println("item getListname: " + itemData.getListname());
		            	  System.out.println("item getItemname: " + itemData.getItemname() + "  items[0]= " + items[0]);
		            	  System.out.println("item getQuantity: " + itemData.getQuantity() + "  items[2]= " + items[1]);
		            	  System.out.println("item getPrice: " + itemData.getPrice() + "  items[2]= " + items[2]);
		            	  System.out.println("item getQuantity: " + itemData.getTotalPrice() + "  items[3]= " + items[3]);
		            	  DbHelper.open();
		            	  
		            	  
		            	  
		            	  DbHelper.addItem(itemData);
	            		  DbHelper.close();
            			  items = new String[4];
            			  j=0;
            			  items[j] = cell.getCell().getValue().toString();
	            	  }
	            	  j++;
		              
	            }
            	  item_data itemData = new item_data(list_name,items[0],items[1],items[2],items[3]);
            	  System.out.println("item getListname: " + itemData.getListname());
            	  System.out.println("item getItemname: " + itemData.getItemname() + "  items[0]= " + items[0]);
            	  System.out.println("item getQuantity: " + itemData.getQuantity() + "  items[1]= " + items[1]);
            	  System.out.println("item getPrice: " + itemData.getPrice() + "  items[2]= " + items[2]);
            	  System.out.println("item getQuantity: " + itemData.getTotalPrice() + "  items[3]= " + items[3]);
            	  DbHelper.open();
            	  DbHelper.addItem(itemData);
            	  DbHelper.close();
        		  
	  		}
	    }
        catch (Exception e){
        	Toast.makeText(log_in.this, "Given details of the excel sheet do not exists", Toast.LENGTH_LONG).show();
        	finish();
        }
	}
		
}
	

